<!--车辆分配页面-->
<template>
  <!-- 顶部条 -->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">调度管理</span>
  </div>
  <!-- 审批搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="工单申请人">
        <el-input placeholder="请输入工单申请人" style="width:220px;" v-model="distributeQuery.username"
                  @keydown.enter.prevent="loadDistribute"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadDistribute">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 调度单主体 -->
  <el-card style="margin:20px;">
    <!--  车辆分配状态项  -->
    <el-radio-group style="margin-bottom: 15px;" v-model="type" @change="loadDistribute">
      <el-radio-button value="50" size="large">待分配</el-radio-button>
      <el-radio-button value="60" size="large">已分配</el-radio-button>
    </el-radio-group>
    <!--  调度列表  -->
    <el-table :data="distributeArr">
      <el-table-column label="编号" prop="id" align="center" width="55" type="index"></el-table-column>
      <el-table-column label="工单申请人" prop="username" align="center" width="110"></el-table-column>
      <el-table-column label="申请时间" prop="startTime" align="center"></el-table-column>
      <el-table-column label="出发地" prop="departureAddr" align="center"></el-table-column>
      <el-table-column label="目的地" prop="destinationAddr" align="center"></el-table-column>
      <el-table-column label="用车事由" prop="reason" align="center"></el-table-column>
      <el-table-column label="开始时间" prop="startTime" align="center"></el-table-column>
      <el-table-column label="结束时间" prop="endTime" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center" v-if="type==50||type==60" :key="audit">
        <template #default="scope">
          <!-- 分配50：在待分配选项卡下显示，为申请用车的用户分配用车 -->
          <el-button type="primary" link v-if="type==50" @click="loadGeo(scope.row.id)">分配用车</el-button>
          <!-- 还车60：在已分配选项卡下显示，为即将结束工单的用户还车 -->
          <el-button type="primary" link v-if="type==60" @click="returnVehicle(scope.row.id)">还车</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 围栏车辆概览弹窗 -->
  <el-dialog title="围栏列表" v-model="dialogVisible" style="padding:40px;">
    <el-form>
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="围栏名称:">
            <el-input placeholder="请输入" v-model="geoSearchForm.name" @keydown.enter="loadGeo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="围栏状态">
            <el-select placeholder="请选择" v-model="geoSearchForm.status" @change="loadGeo">
              <el-option label="启用" value="1"/>
              <el-option label="禁用" value="0"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4"></el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button-group>
              <el-button @click="resetGeoSearch">重置</el-button>
              <el-button type="primary" @click="loadGeo">查询</el-button>
            </el-button-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table style="margin:20px 0" :data="geoTableData">
      <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="围栏名称" prop="name" width="200" align="center"></el-table-column>
      <el-table-column label="车辆总数" prop="totalNum" width="200" align="center"></el-table-column>
      <el-table-column label="可用车辆" prop="availableNum" align="center"></el-table-column>
      <el-table-column label="围栏状态" prop="status" align="center" :formatter="statusGeoFormat"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)" :disabled="scope.row.status!=1">查看车辆</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>

  <!-- 分配车辆弹窗 -->
  <el-dialog title="分配车辆" style="padding:40px;" v-model="vehicleDialogVisible">
    <el-table style="margin:20px 0" :data="vehicleTableData">
      <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="车辆品牌" prop="brand" width="200" align="center"></el-table-column>
      <el-table-column label="车牌号" prop="license" width="200" align="center"></el-table-column>
      <el-table-column label="车辆类型" prop="type" align="center"></el-table-column>
      <el-table-column label="车辆状态" prop="status" align="center" :formatter="statusTypeFormat"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="distributeVehicle(scope.row.id)" :disabled="scope.row.status!=1">分配</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

// 判断是否分配用车的变量
let type = ref('50');

/** 查询工单列表业务 */
// 工单列表数组
let distributeArr = ref([]);
// 工单查询条件数组
let distributeQuery = ref({
  type: '',
  username: '',
});
// 获取分配用车列表方法
const loadDistribute = () => {
  distributeQuery.value.type = type.value;
  let data = qs.stringify(distributeQuery.value);
  axios.get(BASE_URL + '/v1/distribute/select?' + data)
      .then((res) => {
        if (res.data.code == 2000) {
          distributeArr.value = res.data.data;
        } else {
          ElMessage.error(res.data.msg)
        }
      })
}
// 重置查询条件
const resetSearch = () => {
  distributeQuery.value.username = '';
  loadDistribute();
}

/** 围栏查看及分配车辆业务 */
// 弹窗显示与否的控制变量
const dialogVisible = ref(false);  // 围栏
const vehicleDialogVisible = ref(false);  // 车辆
// 围栏数据搜索条件数据变量
const geoSearchForm = ref({
  name: '',
  status: ''
});
// 围栏表格数据数组
const geoTableData = ref([]);
// 车辆表格数据数组
const vehicleTableData = ref([]);
// 储存当前工单id
let applicationIdGlobal = 0;
// 定义加载围栏的方法
const loadGeo = (applicationId) => {
  dialogVisible.value = true;
  applicationIdGlobal = applicationId;
  let data = qs.stringify(geoSearchForm.value)
  console.log(data);
  axios.get(BASE_URL + '/v1/geofence/select?' + data)
      .then((res) => {
        if (res.data.code === 2000) {
          geoTableData.value = res.data.data;
        } else {
          ElMessage.error(res.data.msg);
        }
      })
}
// 重置围栏搜索项
const resetGeoSearch = () => {
  geoSearchForm.value = {};
  loadGeo();
}
// 显示车辆列表
const loadVehicle = (geoId) => {
  vehicleDialogVisible.value = true;
  geoTableData.value.forEach((item) => {
    if (item.id == geoId) {
      vehicleTableData.value = item.vehicleList;
    }
  })
}
// 格式化围栏状态
const statusGeoFormat = (row, column, cellValue, index) => {
  switch (row.status) {
    case '1':
      cellValue = '启用';
      break;
    case '0':
      cellValue = '禁用';
      break;
    default:
      cellValue = '未知';
      break;
  }
  return cellValue;
}
// 格式化车辆状态
const statusTypeFormat = (row, column, cellValue, index) => {
  switch (row.status) {
    case '1':
      cellValue = '空闲';
      break;
    case '2':
      cellValue = '占用';
      break;
    default:
      cellValue = '未知';
      break;
  }
  return cellValue;
}
// 分配车辆
const distributeVehicle = (vehicleId) => {
  let data = qs.stringify({
    vehicleId: vehicleId,
    applicationId: applicationIdGlobal
  });
  axios.post(BASE_URL + '/v1/distribute/allocation?'+ data)
      .then((res) => {
        if (res.data.code == 2000) {
          ElMessage.success('已分配车辆!');
          vehicleDialogVisible.value = false;
          dialogVisible.value = false;
          loadDistribute();
        } else {
          ElMessage.error(res.data.msg);
        }
      })
}

/** 还车业务 */
const returnVehicle = (applicationId) => {
  let vehicleId = 0;
  distributeArr.value.forEach((item) => {
    if (item.id == applicationId) {
      vehicleId = item.vehicleId;
    }
  })
  let data = qs.stringify({
    vehicleId: vehicleId,
    applicationId: applicationId
  });
  console.log(data);
  axios.post(BASE_URL + '/v1/distribute/returnVehicle', data)
      .then((res) => {
        if (res.data.code == 2000) {
          ElMessage.success('已归还车辆!');
          loadDistribute();
        } else {
          ElMessage.error(res.data.msg);
        }
      })
}

onMounted(() => {
  loadDistribute();
})
</script>

<style scoped>

</style>